package com.sql.azarias.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sql.azarias.R;
import com.sql.azarias.base.BaseMvpFragment;
import com.sql.azarias.base.BasePresenter;
import com.sql.azarias.entity.BelleEntity;
import com.sql.azarias.entity.LiveEntity_;
import com.sql.azarias.http.EntityRequest;
import com.sql.azarias.http.Result;
import com.sql.azarias.http.SimpleHttpListener;
import com.sql.azarias.ui.fragment.Presenter.LivePresenter;
import com.sql.azarias.views.ToastView;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Administrator on 2017/3/17.
 */

public class BelleFragment extends BaseMvpFragment{
    private Context context;
    List<BelleEntity.ResultsBean> results;
    private CommonAdapter<BelleEntity.ResultsBean> adapter;
    @Bind(R.id.recyclerview_belle)
    RecyclerView recyclerView_belle;
    @Override
    public BasePresenter initPresenter() {
        return new LivePresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_belle;
    }

    @Override
    protected void initInjector() {
        context=getActivity();

    }

    @Override
    protected void initEventAndData() {
        getRequest();
    }
    /**
     * 获取美女图片，这个API不完整，省略了好多参数
     * eg: http://c.3g.163.com/recommend/getChanListNews?channel=T1456112189138&size=40&offset=0
     *
     * @param
     * @return
     */
   public void getRequest(){
       String url = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/60/1";
       EntityRequest<BelleEntity> entityRequest= new EntityRequest(url, RequestMethod.GET,BelleEntity.class);
       entityRequest.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
       request(context,entityRequest, new SimpleHttpListener<BelleEntity>() {
           @Override
           public void onSucceed(int what, Result<BelleEntity> t1) {
               super.onSucceed(what, t1);
               if (t1.isSucceed()) {
                   BelleEntity entity = t1.getResult();
                   List<BelleEntity.ResultsBean> results = entity.getResults();
                 adapter = new CommonAdapter<BelleEntity.ResultsBean>(context,R.layout.item_belle,results) {
                     @Override
                     protected void convert(ViewHolder holder, BelleEntity.ResultsBean resultsBean, int position) {
                         ImageView imageView = holder.getView(R.id.item_bell_img);
                         Glide.with(context)
                                 .load(resultsBean.getUrl())
                                 .into(imageView);
                     }
                 };
                /*   adapter=new CommonAdapter<BelleEntity>(context,R.layout.item_belle,results) {
                       @Override
                       protected void convert(ViewHolder holder, BelleEntity belleEntity, int position) {
                           ImageView imageView = holder.getView(R.id.item_bell_img);
                           Glide.with(context)
                                   .load(belleEntity.getResults().get)
                                   .into(imageView);
                       }
                   };*/
                   recyclerView_belle.setAdapter(adapter);
                   ToastView.getInstance().show(entity.getResults().get(1).getUrl(),context);
               }else{
                   ToastView.getInstance().show(t1.getError(),context);
               }
           }
       });
    }
}
